using Microsoft.Extensions.Configuration;
using JGSY.CMS.LowCode.Platform.Infrastructure.Configuration.Models;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Configuration;

/// <summary>
/// 静态配置访问器 - 提供全局静态访问配置的能力
/// 所有配置访问都通过此类进行，确保配置管理的统一性
/// </summary>
public static class ConfigurationAccessor
{
    private static EnhancedConfigurationMappingFactory? _factory;
    private static IConfiguration? _configuration;

    /// <summary>
    /// 初始化配置访问器
    /// </summary>
    /// <param name="configuration">配置对象</param>
    public static void Initialize(IConfiguration configuration)
    {
        _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
        _factory = new EnhancedConfigurationMappingFactory(configuration);
    }

    /// <summary>
    /// 获取配置工厂实例
    /// </summary>
    public static EnhancedConfigurationMappingFactory Factory
    {
        get
        {
            if (_factory == null)
                throw new InvalidOperationException("ConfigurationAccessor未初始化，请先调用Initialize方法");
            return _factory;
        }
    }

    #region 常用配置属性 - 静态访问

    /// <summary>
    /// Serilog自定义配置 - 是否强制启用
    /// </summary>
    public static bool SerilogForceToEnable => Factory.GetSerilogCustomValue<bool>("ForceToEnable", false);

    /// <summary>
    /// Serilog自定义配置 - 数据库序列化类型
    /// </summary>
    public static string SerilogDatabaseSerialType => Factory.GetSerilogCustomValue<string>("DatabaseSerialType", "SQLite") ?? "SQLite";

    /// <summary>
    /// 主数据库类型
    /// </summary>
    public static string MainDatabaseType => Factory.GetDatabaseType();

    /// <summary>
    /// 权限功能是否启用
    /// </summary>
    public static bool PermissionEnabled => Factory.GetPermissionConfiguration().Enabled;

    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    public static string DefaultConnectionString => Factory.GetConnectionString("DefaultConnection");

    /// <summary>
    /// Redis连接字符串
    /// </summary>
    public static string RedisConnectionString => Factory.GetRedisConnectionString();

    /// <summary>
    /// 上传文件路径
    /// </summary>
    public static string UploadPath => Factory.GetUploadPath();

    /// <summary>
    /// 上传文件基础URL
    /// </summary>
    public static string UploadBaseUrl => Factory.GetUploadBaseUrl();

    /// <summary>
    /// 环境名称
    /// </summary>
    public static string EnvironmentName => Factory.GetEnvironmentName();

    /// <summary>
    /// 配置版本
    /// </summary>
    public static string ConfigurationVersion => Factory.GetConfigurationVersion();

    #endregion

    #region 配置对象访问 - 静态方法

    /// <summary>
    /// 获取数据库配置
    /// </summary>
    public static DatabaseConfiguration DatabaseConfig => Factory.GetDatabaseConfiguration();

    /// <summary>
    /// 获取日志配置
    /// </summary>
    public static LoggingConfiguration LoggingConfig => Factory.GetLoggingConfiguration();

    /// <summary>
    /// 获取缓存配置
    /// </summary>
    public static CacheConfiguration CacheConfig => Factory.GetCacheConfiguration();

    /// <summary>
    /// 获取CORS配置
    /// </summary>
    public static CorsConfiguration CorsConfig => Factory.GetCorsConfiguration();

    /// <summary>
    /// 获取权限配置
    /// </summary>
    public static PermissionConfiguration PermissionConfig => Factory.GetPermissionConfiguration();

    /// <summary>
    /// 获取上传配置
    /// </summary>
    public static UploadConfiguration UploadConfig => Factory.GetUploadConfiguration();

    /// <summary>
    /// 获取云存储配置
    /// </summary>
    public static CloudStorageConfiguration CloudStorageConfig => Factory.GetCloudStorageConfiguration();

    /// <summary>
    /// 获取Serilog自定义配置
    /// </summary>
    public static SerilogCustConfiguration SerilogCustConfig => Factory.GetSerilogCustConfiguration();

    /// <summary>
    /// 获取完整应用配置
    /// </summary>
    public static EnhancedAppSettingsConfiguration CompleteConfig => Factory.GetCompleteApplicationSettings();

    #endregion

    #region 通用配置值访问

    /// <summary>
    /// 获取配置值 - 通用方法
    /// </summary>
    /// <typeparam name="T">配置值类型</typeparam>
    /// <param name="key">配置键</param>
    /// <param name="defaultValue">默认值</param>
    /// <returns>配置值</returns>
    public static T GetValue<T>(string key, T defaultValue = default!)
    {
        if (_configuration == null)
            throw new InvalidOperationException("ConfigurationAccessor未初始化，请先调用Initialize方法");
        
    var value = _configuration.GetValue<T>(key, defaultValue);
    return value is null ? defaultValue : value;
    }

    /// <summary>
    /// 获取连接字符串
    /// </summary>
    /// <param name="name">连接字符串名称</param>
    /// <returns>连接字符串</returns>
    public static string GetConnectionString(string name)
    {
        return Factory.GetConnectionString(name);
    }

    #endregion
}
